/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.mzk.kramerius.articles.importFromLn.articlesData;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author x211800
 */
public abstract class CsvParser implements ArticleDataParser{

    private static final char CSV_TEXT_SEPARATOR = '\"';
    private static final String EMPTY_STRING = "";
    private static final Logger logger = Logger.getLogger(CsvParser.class.getName());
    private static final char CSV_FIELD_SEPARATOR = '\t';


    /**
     *
     * @param string
     * @return for "text" returns text (if CSV_TEXT_SEPARATOR is '"') and null for empty string
     */
    protected String removeTextDelimits(String string) {
        //System.out.println("string: " + string);
        if ("".equals(string)) {
            return null;
        }
        int size = string.length();
        //System.out.println("size: " + size);
        if (string.charAt(0) != CSV_TEXT_SEPARATOR || string.charAt(size - 1) != CSV_TEXT_SEPARATOR) {
            logger.log(Level.SEVERE, "String ''{0}'' not quoted with ''{1}''", new Object[]{string, CSV_TEXT_SEPARATOR});
            return null;
        }
        return string.substring(1, size - 1);
    }

    /**
     *
     * @param string
     * @return for text returns "text" (if CSV_TEXT_SEPARATOR is '"')
     * for {@code string == null} returns null, for {@code string == ""} returns ""
     */
    protected String addTextDelimits(String string) {
        if (string == null) {
            return null;
        }
        if (string.equals(EMPTY_STRING)) {
            return EMPTY_STRING;
        }
        StringBuilder builder = new StringBuilder(3);
        builder.append(CSV_TEXT_SEPARATOR);
        builder.append(string);
        builder.append(CSV_TEXT_SEPARATOR);
        return builder.toString();
    }

protected String createHeaderLine() {
        StringBuilder builder = new StringBuilder(28);
        builder.append(addTextDelimits("Identifikátor"));
        builder.append(CSV_FIELD_SEPARATOR);
        builder.append(addTextDelimits("Žánr"));
        builder.append(CSV_FIELD_SEPARATOR);
        builder.append(addTextDelimits("Jazyk (cz/de/jiný)"));
        builder.append(CSV_FIELD_SEPARATOR);
        builder.append(addTextDelimits("Autor"));
        builder.append(CSV_FIELD_SEPARATOR);
        builder.append(addTextDelimits("Název"));
        builder.append(CSV_FIELD_SEPARATOR);
        builder.append(addTextDelimits("Podnázev"));
        builder.append(CSV_FIELD_SEPARATOR);
        builder.append(addTextDelimits("[] (Info)"));
        builder.append(CSV_FIELD_SEPARATOR);
        builder.append(addTextDelimits("Číslo ročníku"));
        builder.append(CSV_FIELD_SEPARATOR);
        builder.append(addTextDelimits("Seznam datumů"));
        builder.append(CSV_FIELD_SEPARATOR);
        builder.append(addTextDelimits("Seznam čísel vydání"));
        builder.append(CSV_FIELD_SEPARATOR);
        builder.append(addTextDelimits("Seznam druhů vydání (1.ranní/ranní/odpolední/malé)"));
        builder.append(CSV_FIELD_SEPARATOR);
        builder.append(addTextDelimits("Seznam stran/intervalů stran"));
        builder.append(CSV_FIELD_SEPARATOR);
        builder.append(addTextDelimits("Poznámka"));
        builder.append(CSV_FIELD_SEPARATOR);
        builder.append(addTextDelimits("Chybové zprávy"));
        builder.append('\n');
        return builder.toString();
    }


}
